package com.huitone.gddw.service.impl.worktable;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.huitone.gddw.common.ComConstant;
import com.huitone.gddw.dao.entity.SmModuleVar;
import com.huitone.gddw.dao.worktable.paramconfigmanage.ISmModuleVarDao;
import com.huitone.gddw.model.support.QueryPage;
import com.huitone.gddw.service.IParamtabRecService;
import com.huitone.gddw.service.ISmModuleVarService;
import com.huitone.gddw.utils.SysLoginUtil;

/**
 * 短接模板接口
 * @author Weiliyu
 *
 */
@Service
public class SmModuleVarService implements ISmModuleVarService {

	@Autowired
	private ISmModuleVarDao smModuleVarDao;
	
	@Autowired
	private IParamtabRecService paramtabRecService;
	
	@Override
	public QueryPage<SmModuleVar> selectPage(QueryPage<SmModuleVar> page) {
		Map<String, Object> map = new HashMap<>();
		map.put("svrNode", page.getParams().get("svrNode"));
		map.put("startDate", page.getParams().get("startDate"));
		map.put("endDate", page.getParams().get("endDate"));
		map.put("start", (page.getPageNo() - 1) * page.getPageSize() + 1);
		map.put("end", page.getPageNo() * page.getPageSize());
		List<SmModuleVar> result = smModuleVarDao.selectPage(map);
		int totalCount = smModuleVarDao.selectCount(map);
		
		QueryPage<SmModuleVar> queryPage = new QueryPage<>();
		queryPage.setResult(result);
		queryPage.setPageNo(page.getPageNo());
		queryPage.setPageSize(page.getPageSize());
		queryPage.setTotalCount(totalCount);
		return queryPage;
	}

	@Override
	public SmModuleVar selectByPrimaryKey(String smTempletId) {
		return smModuleVarDao.selectByPrimaryKey(smTempletId);
	}
	
	@Override
	public void save(SmModuleVar entity) {
		
		entity.setStatus("1"); //默认有效
		entity.setInsertTime(new Date());
		entity.setInsertStaffId(SysLoginUtil.getLoginUserId());
		entity.setLastModTime(entity.getInsertTime());
		entity.setLastModStaffId(entity.getInsertStaffId());
		smModuleVarDao.insertSelective(entity);
		this.updateParamRec("保存短信模板变量信息");
	}

	@Override
	public void update(SmModuleVar entity) {
		entity.setLastModTime(new Date());
		entity.setLastModStaffId(SysLoginUtil.getLoginUserId());
		smModuleVarDao.updateByPrimaryKey(entity);
		this.updateParamRec("更新短信模板变量信息");
	}

	@Override
	public SmModuleVar toUpdate(String smTempletId) {
		return this.selectByPrimaryKey(smTempletId);
	}

	@Override
	public void delete(String smTempletId) {
		smModuleVarDao.deleteByPrimaryKey(smTempletId);
		this.updateParamRec("删除短信模板变量信息");
	}
	
	/**
	 * 更新参数记录表
	 * @param desc 说明
	 */
	private void updateParamRec(String desc) {
		paramtabRecService.update(ComConstant.SM_MODULE, desc);
	}

	@Override
	public List<SmModuleVar> selectAllVar() {
		return smModuleVarDao.selectAllVar();
	}

	@Override
	public List<SmModuleVar> selecVartBySvrNode(String svrNode) {
		
		return  smModuleVarDao.selecVartBySvrNode(svrNode);
		
		  
	}

	@Override
	public List<SmModuleVar> selecVartByMoVar(SmModuleVar entity) {
		return smModuleVarDao.selecVartByMoVar(entity);
	}
	
	@Override
	public List<SmModuleVar> selecVarBySvrAndCom(String svrNode) {
		
		return  smModuleVarDao.selecVarBySvrAndCom(svrNode);
		
		  
	}
	
	@Override
	public List<SmModuleVar> selecVarBySet(String svrNode) {
		
		List<SmModuleVar> list=  smModuleVarDao.selecVartBySvrNode(svrNode);
		
		 for(SmModuleVar svar:list){
			 if (svar.getSvrNode().equals("00000")) {
				svar.setSvrNodeName("公共");
			}
		 }
		 
//		HashMap<String,List<SmModuleVar>> hashMap = new HashMap<>();
//		
//		 for(SmModuleVar svar:list){
//			if(hashMap.get(svar.getSvrNodeName())==null){
//				List<SmModuleVar> lists = new ArrayList<>();
//				lists.add(svar);
//				hashMap.put(svar.getSvrNodeName(), lists);
//			}else{
//				 hashMap.get(svar.getSvrNodeName()).add(svar);
//			}	
//		 }
		 return list;
		 
	}
	
}
